﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web4Comment.Service
{
    public class TimeService
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="start_time">开始时间</param>
        /// <param name="type_1">1.好友交流;2.浏览新闻;3.维护个人信息</param>
        /// <param name="type_2">1.好友id;2.新闻id;3.没有意义</param>
        /// <returns>timeid</returns>
        protected static long logStartTimeBase(int userid, DateTime start_time, int type_1, int type_2)
        {
            UnlessComment.BLL.Time timeBLL = new UnlessComment.BLL.Time();
            UnlessComment.Model.Time timeModel = new UnlessComment.Model.Time();
            timeModel.userid = userid;
            timeModel.end_time = start_time;    //0
            timeModel.start_time = start_time;
            timeModel.type_1 = type_1;
            timeModel.type_2 = type_2;
            timeModel.state = 1;
            return timeBLL.Add(timeModel);
        }

        /// <summary>
        /// 结束时间
        /// </summary>
        /// <param name="timeid"></param>
        /// <param name="end_time"></param>
        /// <returns>timeid</returns>
        protected static bool logEndTimeBase(long timeid, DateTime end_time)
        {
            UnlessComment.BLL.Time timeBLL = new UnlessComment.BLL.Time();
            UnlessComment.Model.Time timeModel = timeBLL.GetModel(timeid);
            if (timeModel == null)
                return false;
            timeModel.end_time = end_time;
            return timeBLL.Update(timeModel);
        }

        /// <summary>
        /// 计算时间
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="type_1">1.好友交流;2.浏览新闻;3.维护个人信息</param>
        /// <param name="type_2">1.好友id;2.新闻id;3.没有意义</param>
        /// <returns>timeid</returns>
        protected static TimeSpan calculateTimeBase(int userid, int type_1, int type_2)
        {
            UnlessComment.BLL.Time timeBLL = new UnlessComment.BLL.Time();
            List<UnlessComment.Model.Time> timeModelList = timeBLL.GetModelList(
                "userid = " + userid + " and type_1 = " + type_1 + " and type_2 = " + type_2);
            TimeSpan totalTime = new TimeSpan(0, 0, 0, 0, 0);
            for (int i = 0; i < timeModelList.Count; i++)
            {
                totalTime += calculateSingleTimeBase(timeModelList[i].id);
            }

            return totalTime;
        }

        /// <summary>
        /// 计算时间
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="type_1">1.好友交流;2.浏览新闻;3.维护个人信息</param>
        /// <returns>timeid</returns>
        protected static TimeSpan calculateTimeTotalBase(int userid, int type_1)
        {
            UnlessComment.BLL.Time timeBLL = new UnlessComment.BLL.Time();
            List<UnlessComment.Model.Time> timeModelList = timeBLL.GetModelList(
                "userid = " + userid + " and type_1 = " + type_1);
            TimeSpan totalTime = new TimeSpan(0, 0, 0, 0, 0);
            for (int i = 0; i < timeModelList.Count; i++)
            {
                totalTime += calculateSingleTimeBase(timeModelList[i].id);
            }

            return totalTime;
        }

        /// <summary>
        /// 计算单一时间
        /// </summary>
        /// <param name="timeid">时间id</param>
        /// <returns></returns>
        private static TimeSpan calculateSingleTimeBase(long timeid)
        {
            UnlessComment.BLL.Time timeBLL = new UnlessComment.BLL.Time();
            UnlessComment.Model.Time timeModel = timeBLL.GetModel(timeid);
            TimeSpan dateTime = timeModel.end_time.Value - timeModel.start_time.Value;
            return dateTime;
        }
    }
}
